/**
 * 
 */
package hihocoder;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author zhongfang
 *
 */
public class 是二叉搜索树吗 {

	static class Node {
		public Node(int j) {
			this.val = j;
		}

		int val;
		ArrayList<Node> childs = new ArrayList<>();
		ArrayList<Node> parent = new ArrayList<>();
		Node left;
		Node right;
	}

	static Node root;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int T = scanner.nextInt();
		for (int i = 0; i < T; i++) {
			int N = scanner.nextInt();// 节点数
			Node[] nodes = new Node[N];
			for (int j = 0; j < nodes.length; j++) {
				nodes[j] = new Node(j + 1);
			}
			for (int j = 0; j < N - 1; j++) {
				int a = scanner.nextInt() - 1;
				int b = scanner.nextInt() - 1;
				nodes[a].childs.add(nodes[b]);
				nodes[b].parent.add(nodes[a]);
			}
			if (!isTree(nodes)) {
				System.out.println("ERROR1");
				continue;
			}
			if (!isBinaryTree(nodes)) {
				System.out.println("ERROR2");
				continue;
			}
			if ((!isbst) || !isBST(root, new int[2])) {
				System.out.println("ERROR3");
				continue;
			}
			StringBuilder sBuilder = seq(root);
			System.out.println(sBuilder.toString());
		}

	}

	private static StringBuilder seq(Node root2) {
		StringBuilder sBuilder = new StringBuilder();// "(" + root2.val +
														// seq(root2.left) +
														// seq(root2.right) +
														// ")"
		if (root2 == null) {
			sBuilder.append("()");
			return sBuilder;
		}
		sBuilder.append("(");
		sBuilder.append(root2.val);
		sBuilder.append(seq(root2.left));
		sBuilder.append(seq(root2.right));
		sBuilder.append(")");
		return sBuilder;
	}

	/**
	 * 判断是BST 左子最大<cur<右子最小
	 * 
	 * @param nodes
	 * @param is
	 * @return
	 */
	private static boolean isBST(Node root, int[] m) {
		m[0] = m[1] = root.val;

		if (root.left != null) {
			int[] l = new int[2];
			if (!isBST(root.left, l)) {
				return false;
			}
			if (l[1] > root.val) {
				return false;
			}
			m[0] = l[0];
		}
		if (root.right != null) {
			int[] r = new int[2];
			if (!isBST(root.right, r)) {
				return false;
			}
			if (r[0] < root.val) {
				return false;
			}
			m[1] = r[1];
		}
		return true;
	}

	static boolean isbst = true;

	/**
	 * 判断是否二叉树 每个节点只有<3个子节点
	 * 
	 * @param nodes
	 * @return
	 */
	private static boolean isBinaryTree(Node[] nodes) {
		isbst = true;
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].childs.size() > 2) {
				return false;
			}
			for (Node chi : nodes[i].childs) {// 指定左右子
				if (chi.val < nodes[i].val) {
					if (nodes[i].left != null) {// 如果两个子都是小或者大，则非BST
						isbst = false;
						continue;
					}
					nodes[i].left = chi;
				} else {
					if (nodes[i].right != null) {
						isbst = false;
						continue;
					}
					nodes[i].right = chi;
				}
			}
		}
		return true;
	}

	private static boolean isTree(Node[] nodes) {// 是树，每个点只有一个父亲，根没有父亲，只有一个根
		int rootCnt = 0;
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].parent.size() > 1) {
				return false;
			}
			if (nodes[i].parent.isEmpty()) {
				rootCnt++;
				root = nodes[i];// 根
			}
		}
		if (rootCnt != 1) {
			return false;
		}

		// 递归要求不能有相同的根
		for (int i = 0; i < nodes.length; i++) {
			Node iRoot = getRoot(nodes[i]);
			if (iRoot == null && nodes[i] != root) {
				return false;
			}
			if (iRoot == nodes[i]) {
				return false;
			}
		}
		return true;
	}

	private static Node getRoot(Node node) {
		if (node == null || node.parent.get(0).equals(node)) {
			return node;
		}
		return getRoot(node.parent.get(0));
	}

}
